package com.baidu.foodrecipe.data.local

import androidx.room.TypeConverter
import com.baidu.foodrecipe.data.model.FoodRecipe
import com.baidu.foodrecipe.data.model.Result
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class FoodRecipeConverter {
    private val gson = Gson()

    //FoodRecipe -> String
    @TypeConverter
    fun foodRecipeToString(foodRecipe: FoodRecipe): String{
        return gson.toJson(foodRecipe)
    }

    //String -> FoodRecipe
    @TypeConverter
    fun stringToFoodRecipe(recipeString: String): FoodRecipe{
        val type = object : TypeToken<FoodRecipe>(){}.type
        return gson.fromJson<FoodRecipe>(recipeString, type)
    }

    //Result -> String
    @TypeConverter
    fun resultToString(result: Result): String{
        return gson.toJson(result)
    }

    //String -> Result
    @TypeConverter
    fun stringToResult(recipeString: String): Result{
        val type = object : TypeToken<Result>(){}.type
        return gson.fromJson<Result>(recipeString, type)
    }
}